﻿using System;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace EasyStudy.Logic.Email
{
    public static class StringMethodExtensions
    {
        private static string _paraBreak = "\r\n\r\n";
        private static string _link = "<a href=\"{0}\">{1}</a>";
        private static string _linkNoFollow = "<a href=\"{0}\" rel=\"nofollow\">{1}</a>";

        public static string ToHtml(string s)
        {
            return ToHtml(s, false);
        }

        /// <summary>
        /// Returns a copy of this string converted to HTML markup.
        /// </summary>
        /// <param name="nofollow">If true, links are given "nofollow"
        /// attribute</param>
        public static string ToHtml(string s, bool nofollow)
        {
            StringBuilder sb = new StringBuilder();

            int pos = 0;
            while (pos < s.Length)
            {
                // Extract next paragraph
                int start = pos;
                pos = s.IndexOf(_paraBreak, start);
                if (pos < 0)
                    pos = s.Length;
                string para = s.Substring(start, pos - start).Trim();

                // Skip over paragraph break
                pos += _paraBreak.Length;
            }
            // Return result
            return sb.ToString();
        }


        /// <summary>
        /// Encodes [[URL]] and [[Text][URL]] links to HTML.
        /// </summary>
        /// <param name="text">Text to encode</param>
        /// <param name="sb">StringBuilder to write results</param>
        /// <param name="nofollow">If true, links are given "nofollow"
        /// attribute</param>
        private static void EncodeLinks(string s, StringBuilder sb, bool nofollow)
        {
            // Parse and encode any hyperlinks
            int pos = 0;
            while (pos < s.Length)
            {
                // Look for next link
                int start = pos;
                pos = s.IndexOf("[[", pos);
                if (pos < 0)
                    pos = s.Length;
                // Copy text before link
                sb.Append(s.Substring(start, pos - start));
                if (pos < s.Length)
                {
                    string label, link;

                    start = pos + 2;
                    pos = s.IndexOf("]]", start);
                    if (pos < 0)
                        pos = s.Length;
                    label = s.Substring(start, pos - start);
                    int i = label.IndexOf("][");
                    if (i >= 0)
                    {
                        link = label.Substring(i + 2);
                        label = label.Substring(0, i);
                    }
                    else
                    {
                        link = label;
                    }
                    // Append link
                    sb.Append(String.Format(nofollow ? _linkNoFollow : _link, link, label));

                    // Skip over closing "]]"
                    pos += 2;
                }
            }
        }
    }

    public class EmailSender
    {
        public EmailSender()
        {
            from = new MailAddress("EasyStudy2013@gmail.com");
            body = new StringBuilder();
            userName = "EasyStudy2013@gmail.com";
            password = "zhoufeng";
            picURL = System.AppDomain.CurrentDomain.BaseDirectory + "EasyStudy.gif";
        }

        public string EmailBody
        {
            get { return body.ToString(); }
            set
            {
                body.Append(value);
                body.Append("<br><br><hr>");
                body.Append("this email is sent by administrator of EasyStudy<br/><br/>");
                body.Append("<img alt='EasyStudy' src=cid:ewen>");
            }
        }

        public void SendMail(string subject, string to, string cc, string bcc)
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

            mail.From = new MailAddress("EasyStudy_Admin@gmail.com");
            if (to != null && to != "")
            {
                mail.To.Add(to);
            }

            if (cc != null && cc != "")
            {
                mail.CC.Add(cc);
            }

            if (bcc != null && bcc != "")
            {
                mail.Bcc.Add(bcc);
            }

            mail.Subject = subject;
            mail.IsBodyHtml = true;

            //添加附件
            mail.Attachments.Add(new Attachment(picURL));
            //配置附件类型
            mail.Attachments[0].ContentType.Name = "image/gif";
            //设置附件ID,邮件正文会用到
            mail.Attachments[0].ContentId = "ewen";
            //设置是否内联
            mail.Attachments[0].ContentDisposition.Inline = true;
            mail.Attachments[0].TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
            mail.Body = body.ToString();


            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential(userName, password);
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);
        }

        private string userName;
        private string password;
        private MailAddress from;
        private StringBuilder body;
        private string picURL;
    }
}